package com.hcbd.tdwt.ui.xingyu.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.core.content.ContextCompat;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.model.GradientColor;

import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.util.ChartUtil;
import com.hcbd.tdwt.view.CompassView;
import com.pangu.bdsdk2021.bluetooth.BluetoothManage;
import com.pangu.bdsdk2021.entity.terminalone.BDVRInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDSnrInfo;
import com.pangu.bdsdk2021.terminal.TerminalSendManager;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.hcbd.tdwt.ui.xingyu.vo.BDBoxVO;
import com.hcbd.tdwt.ui.xingyu.vo.BDBoxVO_Table;
import com.hcbd.tdwt.ui.setting.xingyu.activity.BdDeviceSOSActivity_;
import com.hcbd.tdwt.ui.setting.xingyu.activity.BluetoothNameSetActivity_;
import com.hcbd.tdwt.ui.setting.xingyu.activity.PositionUpActivity_;
import com.hcbd.tdwt.ui.setting.xingyu.activity.RnssFrequencyActivity_;
import com.hcbd.tdwt.ui.setting.xingyu.activity.VoiceParameterActivity_;
import com.hcbd.tdwt.ui.setting.xingyu.activity.WorkAgreeActivity_;
import com.hcbd.tdwt.ui.setting.xingyu.activity.WorkModeActivity_;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_bd_status)
public class BdStatusActivity  extends BaseActivity {
    @ViewById
    Button onDisconnectBtn;
    @ViewById
    BarChart mBarChart;
    //卡号,频度,通信等级,电量
    @ViewById
    TextView boxCardNumTv,boxFrequencyTv,comm_class,electricityTv,cardType;
    //罗盘
    @ViewById
    CompassView compassView;
    @ViewById
    RelativeLayout positionUpRL,sosRL,voiceParameterRL,workAgreeRL;
    @ViewById
    RelativeLayout operatingModeRL,rnssSetRL;
    @ViewById(R.id.bluetooth_name)
    TextView bluetoothName;

    @ViewById(R.id.card_mode)
    TextView cardMode;
    @ViewById
    TextView adders;
    @ViewById(R.id.vr_info)
    TextView vrInfoTv;
    @ViewById
    LinearLayout bluetoothSetLL,powerLL;
    //private List<BarEntry> chartData;


    //开始角度
    private int fromDegrees = 0;

    private int width;
    private ChartUtil chartUtil;


    @AfterViews
    protected void init() {
        initMsgRegister();
        chartUtil=new ChartUtil(this,mBarChart);
        chartUtil.setBottomColor(R.color.bright_green);
        chartUtil.setTopColor(R.color.light_green);
        chartUtil.setLeftTextColor(R.color.white);
        chartUtil.setxGridColor(R.color.dark_green);
        chartUtil.setxTvColor(R.color.white);
        chartUtil.initChart();
        initCompassView();
        initData();
        if(BdToolFactory.MOBILE_PHONE_TYPE==BdToolFactory.SATELLITE_MOBILE_PHONE){
            bluetoothSetLL.setVisibility(View.GONE);
            powerLL.setVisibility(View.GONE);
        }else {
            TerminalSendManager.getInstance().level_two_getVersionInfo();
        }
        refreshStatus();
        chartUtil.updateChartData();
    }
    public void initData(){
        BDBoxVO bdBoxVO= SQLite.select().from(BDBoxVO.class).where(BDBoxVO_Table.bluetoothName.eq(BeidouBoxParams.blueName)).querySingle();
        if(bdBoxVO!=null) {
            bluetoothName.setText(bdBoxVO.bluetoothName);
            cardMode.setText(bdBoxVO.cardMode);
            adders.setText(bdBoxVO.macAdders);
        }
    }
    /**
     * 初始化罗盘
     */
    private void initCompassView() {
        compassView.init();
        compassView.setOnAnglaChanged(angla -> {
            //让图片相对自身中心点转动，开始角度默认为0；此后开始角度等于上一次结束角度
            RotateAnimation ra = new RotateAnimation(fromDegrees, angla, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            ra.setInterpolator(new LinearInterpolator());
            //动画时间200毫秒
            ra.setDuration(Math.abs(fromDegrees - angla));
            ra.setFillAfter(true);
            compassView.startAnimation(ra);
            fromDegrees = angla;
        });
    }
    public void updateChart(){
        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(BeidouBoxParams.horizontalAxis);
        YAxis leftAxis = mBarChart.getAxisLeft();
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(BeidouBoxParams.verticalAxis);
    }



    /**
     * 初始化终端状态
     */
    @SuppressLint("SetTextI18n")
    private void refreshStatus() {
        if (!BeidouBoxParams.isBoxConnectNormal) {
            //终端未连接
            //波束 todo
            boxCardNumTv.setText("未知");
            boxFrequencyTv.setText("未知");
            comm_class.setText("未知");
            electricityTv.setText("未知");
            cardType.setText("未知");
        } else {
            //终端已连接
            //波束 todo
            if (BeidouBoxParams.userCardNumber == null) {
                boxCardNumTv.setText("");cardType.setText("未知");
            } else {
                boxCardNumTv.setText(BeidouBoxParams.userCardNumber);
                String type = BeidouBoxParams.userCardNumber.length()>6?"北斗三号":"北斗二号";
                cardType.setText(type);
            }
            if (BeidouBoxParams.sendFreq != -1) {
                boxFrequencyTv.setText(BeidouBoxParams.sendFreq + "秒卡");
            } else {
                boxFrequencyTv.setText("未知");
            }
            comm_class.setText(String.valueOf(BeidouBoxParams.cardType));
            electricityTv.setText(MessageFormat.format("{0}%", BeidouBoxParams.kwh));
        }
    }

    @Click
    void headLeft(){
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDSnrInfo bdSnrInfo){
        chartUtil.updateChartData();
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDVRInfo bdvrInfo){
        vrInfoTv.setText(bdvrInfo.instruc);
    }
    //位置上报
    @Click
    void positionUpRL(){
        Intent intent=new Intent();
        intent.setClass(this, PositionUpActivity_.class);
        startActivity(intent);
    }

    //报警设置
    @Click
    void sosRL(){
        Intent intent=new Intent();
        intent.setClass(this, BdDeviceSOSActivity_.class);
        startActivity(intent);
    }
    //工作协议
    @Click
    void workAgreeRL(){
        Intent intent=new Intent();
        intent.setClass(this, WorkAgreeActivity_.class);
        startActivity(intent);
    }
    //语音参数设置
    @Click
    void voiceParameterRL(){
        Intent intent=new Intent();
        intent.setClass(this, VoiceParameterActivity_.class);
        startActivity(intent);
    }
    @Click
    void operatingModeRL(){
        Intent intent=new Intent();
        intent.setClass(this, WorkModeActivity_.class);
        startActivity(intent);
    }
    @Click
    void bluetoothNameRL(){
        Intent intent=new Intent();
        intent.setClass(this, BluetoothNameSetActivity_.class);
        startActivity(intent);
    }
    @Click
    void rnssSetRL(){
        Intent intent=new Intent();
        intent.setClass(this, RnssFrequencyActivity_.class);
        startActivity(intent);
    }
    @Click
    void onDisconnectBtn(){
        BluetoothManage.getInstance().disConnect();
        BeidouBoxParams.init();
        finish();
    }
}
